package com.jzy.stock.metadata;

import com.jzy.stock.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 贾志宇
 * @create 2022-07-05-10:37
 */
@RestController
@CrossOrigin
//@RequestMapping("subClass")
@Api(value = "MetadataController",description = "所有页面公共功能接口：下拉框")
public class MetadataController {

    @PostMapping("metadata/getPullDownList")
    @ApiOperation("下拉框接口")
    public ResultVo queryXiala(@RequestBody List<String> tmp){
        //前端接受其想要的下拉列表参数
        Map<String,List<XiaLaVo1>> stringListMap = new HashMap<>();
        //后端返回其想要的下拉列表参数
        Map<String,List<XiaLaVo1>> returnMap = new HashMap<>();
        //1.状态下拉框
        List<XiaLaVo1> statusList = new ArrayList<>();
        for(StatusEnum statusEnum : StatusEnum.values()){
            XiaLaVo1 XiaLaVo1 = new XiaLaVo1(statusEnum.getId(), statusEnum.getName());
            statusList.add(XiaLaVo1);
        }
        stringListMap.put("statusList",statusList);

        //2.价格分类下拉框
        List<XiaLaVo1> priceClassifyList = new ArrayList<>();
        for(PriceClassifyEnum priceClassifyEnum : PriceClassifyEnum.values()){
            XiaLaVo1 XiaLaVo1 = new XiaLaVo1(priceClassifyEnum.getId(), priceClassifyEnum.getName());
            priceClassifyList.add(XiaLaVo1);
        }
        stringListMap.put("jizhang",priceClassifyList);

        //3.币别下拉框
        List<XiaLaVo1> currencyCodeList = new ArrayList<>();
        for(CurrencyTypeEnum statusEnum2 : CurrencyTypeEnum.values()){
            XiaLaVo1 XiaLaVo1 = new XiaLaVo1(statusEnum2.getId(), statusEnum2.getName());
            currencyCodeList.add(XiaLaVo1);
        }
        stringListMap.put("money",currencyCodeList);
        if (CollectionUtils.isEmpty(tmp)){
            return ResultBuildVo.success(stringListMap);
        }else {
            for (String qkey:tmp){
                for (String key:stringListMap.keySet()){
                    if (qkey.equals(key)){
                        returnMap.put(key,stringListMap.get(key));
                    }
                }
            }
            return ResultBuildVo.success(returnMap);
        }
    }

    @PostMapping("metadata/updateStatus")
    @ApiOperation("状态修改接口")
    public ResultVo updateStatus(@RequestBody @Valid StatusUpdateVo statusUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultBuildVo.success();
        //return metadataService.updateStatus(statusUpdateVo);
    }
}
